package com.ocom.wxapi.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.access.EntityMjGroupPer;
import com.ocom.common.request.wx.UserAccessPageOnShowRequest;
import com.ocom.common.request.wx.UserAccessPageSetAccessRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.wx.UserAccessPageOnShowResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.GroupListVo;
import com.ocom.common.vo.UserAccessListVo;
import com.ocom.wxapi.client.AccessClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@PreAuthorize("hasRole('ROLE_WX')")
@RequestMapping("/UserAccessPage")
@RestController
public class UserAccessPageController {

    @Autowired
    AccessClientService accessClientService;

    @PostMapping("/onShow")
    public Result onShow(@Valid UserAccessPageOnShowRequest request){

        UserAccessPageOnShowResponse userAccessPageOnShowResponse=new UserAccessPageOnShowResponse();
//        System.out.println(666);
        Map<String, String> pageFormat=new HashMap<>();
        pageFormat.put("pageTitle","人员权限");
        userAccessPageOnShowResponse.setPageFormat(pageFormat);


        Result result=accessClientService.getUserAccessListByArea(request.getCompanyId(),request.getAreaNo());
        if(result.getCode()==200){
            List<UserAccessListVo> list= JSON.parseObject(JSON.toJSONString(result.getData()), List.class);
            userAccessPageOnShowResponse.setUserAccessList(list);
        }
        Result result1=accessClientService.getGroupListByArea(request.getCompanyId(),request.getAreaNo());
        if(result1.getCode()==200){
            List<GroupListVo> list1= JSON.parseObject(JSON.toJSONString(result1.getData()), List.class);
            userAccessPageOnShowResponse.setGroupList(list1);
        }
        Result result2=accessClientService.getAreaInfoById(request.getCompanyId(),request.getAreaNo());

        if(result2.getCode()==200){
            EntityMjAreaInfo entityMjAreaInfo= JSON.parseObject(JSON.toJSONString(result2.getData()), EntityMjAreaInfo.class);
            if(entityMjAreaInfo!=null){
                userAccessPageOnShowResponse.setAreaName(entityMjAreaInfo.getAreaName());
            }
        }
        return ResultUtil.success(userAccessPageOnShowResponse);
    }



    @PostMapping("/setAccess")
    public Result setAccess(@Valid UserAccessPageSetAccessRequest request){
        Result result = accessClientService.setAccess(request);

        request.setPId(request.getUserId());

        if(result.getCode()==200){
            EntityMjGroupPer entityMjGroupPer= JSON.parseObject(JSON.toJSONString(result.getData()), EntityMjGroupPer.class);
            if(entityMjGroupPer!=null){
                Map<String,Object> map=new HashMap<>();
                map.put("userId",entityMjGroupPer.getPId());
                map.put("role_i",entityMjGroupPer.getGroupNo());
                return ResultUtil.info(result.getCode(),result.getMsg(),map);
            }
        }
        return result;
    }





}
